package com.snape.controller;

import com.snape.entity.User;
import com.snape.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by liuqingcan_snape on 2017/3/10.
 */
@Controller
@RequestMapping(value = "user")

public class UserController {
    @Resource
    UserService userService;


    /**
     *   为用户模拟登录
     * @param username
     * @param userpwd
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "login", method = RequestMethod.POST)
    public Map<String,Object> loginAction(@RequestBody Map map){
        Map<String,Object> result = new HashMap<String, Object>();
        String username = "";String userpwd = "";

        Object tempname=map.get("username");
        Object temppwd=map.get("userpwd");
        if (tempname!=null && temppwd != null) {
            username = tempname.toString();
            userpwd = temppwd.toString();
        }

        result = userService.doLogin(username,userpwd);
        return result;
    }


    /**
     * 用户注册
     * @param user
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "register",method = RequestMethod.POST)//,consumes = "application/json"
    public Map<String,Object> registerAction(@RequestBody User user){
        return userService.insertUser(user);
    }

    /**
     * 更新用户信息
     * @param user
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "update_info",method = RequestMethod.POST)
    public Map<String,Object> updateInfoAction(@RequestBody User user){
        return userService.updateUser(user);
    }

    /**
     * 查询用户个人信息
     * @param userid
     * @return
     */
    @RequestMapping(value = "get_user_info",method = RequestMethod.POST)
    public @ResponseBody Map<String,Object> getUserInfoByUserNameAction(@RequestBody Map map){
        int userid = -1;

        Object tempId=map.get("userid");

        if (tempId!=null ){
            userid= Integer.parseInt(tempId.toString());

        }
        return userService.queryById(userid);


    }

    /**
     * 查询 user 的 cookie
     * @param userid
     * @return
     */
    @RequestMapping(value = "get_user_cookie",method = RequestMethod.POST)
    public @ResponseBody Map<String,Object> getCookieAction(@RequestBody Map map){
        int userid = -1;

        Object tempId=map.get("userid");

        if (tempId!=null ){
            userid= Integer.parseInt(tempId.toString());

        }
        return userService.queryCookie(userid);
    }


    /*****************************************/

    /**
     * 插入直播地址
     * @param userid
     * @param roomid
     * @param add
     * @return
     */
    @RequestMapping(value = "insert_live_address",method = RequestMethod.POST)
    public @ResponseBody Map<String,Object> insertLiveAddAction(@RequestBody Map map){
//        int userid,int roomid,String add


        int userid = -1;int roomid = -1; String add = "";

        Object tempId=map.get("userid");
        Object tempPageNumber=map.get("roomid");
        Object tempPostNumber=map.get("add");
        if (tempId!=null && tempPageNumber != null && tempPostNumber != null){
            userid= Integer.parseInt(tempId.toString());
            roomid = Integer.parseInt(tempPageNumber.toString());
            add = tempPostNumber.toString();

        }
        return  userService.insertLiveAddress(userid,roomid,add);






    }

    /**
     * 更新收藏状态
     * @param userid
     * @param roomid
     * @return
     */
    @RequestMapping(value = "update_collect",method = RequestMethod.POST)
    public @ResponseBody Map<String,Object> updateCollectionStatusAction(@RequestBody Map map){

        int userid = -1;int roomid = -1;

        Object tempId=map.get("userid");
        Object tempPageNumber=map.get("roomid");
        if (tempId!=null && tempPageNumber != null){
            userid= Integer.parseInt(tempId.toString());
            roomid = Integer.parseInt(tempPageNumber.toString());
        }
        return userService.updateCollectionStatus(userid,roomid);
    }

    /**
     * 更新观看记录
     * @param userid
     * @param roomid
     * @return
     */
    @RequestMapping(value = "update_record",method = RequestMethod.POST)
    public @ResponseBody Map<String,Object> updateRecordStatusAction(@RequestBody Map map){

        int userid = -1;int roomid = -1;

        Object tempId=map.get("userid");
        Object tempPageNumber=map.get("roomid");
        if (tempId!=null && tempPageNumber != null){
            userid= Integer.parseInt(tempId.toString());
            roomid = Integer.parseInt(tempPageNumber.toString());
        }
        return userService.updateRecordsStatus(userid,roomid);
    }

    /**
     * 更新 是否录播记录
     * @param userid
     * @param roomid
     * @return
     */
    @RequestMapping(value = "update_webcast",method = RequestMethod.POST)
    public @ResponseBody Map<String,Object> updateWebCasteAction(@RequestBody Map map){

        int userid = -1;int roomid = -1;

        Object tempId=map.get("userid");
        Object tempPageNumber=map.get("roomid");
        if (tempId!=null && tempPageNumber != null){
            userid= Integer.parseInt(tempId.toString());
            roomid = Integer.parseInt(tempPageNumber.toString());
        }
        return userService.updateWebcastingStatus(userid,roomid);
    }

    /**
     * 查询该房间,该用户的所有记录
     * @param userid
     * @param roomid
     * @return
     */
    @RequestMapping(value = "get_all_uservideo",method = RequestMethod.POST)
    public @ResponseBody Map<String,Object> queryAllStatusOfUserToVideoAction(@RequestBody Map map){
        int userid = -1;int roomid = -1;

        Object tempId=map.get("userid");
        Object tempPageNumber=map.get("roomid");
        if (tempId!=null && tempPageNumber != null){
            userid= Integer.parseInt(tempId.toString());
            roomid = Integer.parseInt(tempPageNumber.toString());
        }
        return userService.queryAllStatusOfUserToVideo(userid,roomid);
    }






}